package com.itWaiLong.Service.Impl;

import com.itWaiLong.AppException.AppException;
import com.itWaiLong.AppException.GlobalExceptionHandler;
import com.itWaiLong.Dao.Entities.DeleteGroupProject;
import com.itWaiLong.Dao.User;
import com.itWaiLong.Dao.group_project;
import com.itWaiLong.Mapper.GroupProjectMapper;
import com.itWaiLong.Mapper.UserMapper;
import com.itWaiLong.Service.GroupProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GroupProjectServiceImpl implements GroupProjectService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private GroupProjectMapper groupProjectMapper;

    @Override
    public void addGroupProject(group_project groupProject) {

        //判断负责人是否已注册或登录
        User userByUserId = userMapper.findUserByUserId(groupProject.getLeaderId());
        if (userByUserId == null) {
            throw new GlobalExceptionHandler(AppException.FU_ZE_REN_WEI_ZHU_CHE);
        }

        //通过团队名称和项目名称查看项目是否已经存在
        group_project gProject = groupProjectMapper.getProjectByName(groupProject.getGroupName(),groupProject.getProjectName());
        if(gProject != null){
            throw new GlobalExceptionHandler(AppException.XIANG_MU_YI_CUN_ZAI);
        }

        groupProjectMapper.addGroupProject(groupProject);
    }

    /**
     * 删除项目
     * @param
     * @return
     */
    public void delete(DeleteGroupProject deleteGroupProject) {

        int result = groupProjectMapper.delete(deleteGroupProject);
        if(result < 0){
            throw new GlobalExceptionHandler(AppException.MEI_YOU_GAI_XIANG_MU);
        }
    }

    @Override
    public void update(group_project groupProject) {
        int result = groupProjectMapper.update(groupProject);
        if(result < 0){
            throw new GlobalExceptionHandler(AppException.BIAN_JI_SHI_BAI);
        }
    }

    @Override
    public group_project selectProjectById(int projectId) {

        group_project groupProject = groupProjectMapper.selectProjectById(projectId);

        if (groupProject == null) {
            throw new GlobalExceptionHandler(AppException.MEI_YOU_GAI_XIANG_MU);
        }

        return groupProject;
    }

    @Override
    public List<group_project> selectAllProject(String groupName) {
        List<group_project> groupProjects = groupProjectMapper.selectAllProject(groupName);
        if (groupProjects == null || groupProjects.size() == 0) {
            throw new GlobalExceptionHandler(AppException.MEI_YOU_REN_HE_XIANG_MU);
        }
        return groupProjects;
    }
}
